package com.shiguiwu.springmybatis.spring.transaction;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.NamedThreadLocal;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.TransactionSynchronization;

import javax.sql.DataSource;
import java.util.Map;
import java.util.Set;

/**
 * @description: 事务管理
 * @author: stone
 * @date: Created by 2021/6/14 15:56
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.transaction
 * 注释掉了，跑整个项目会影响的
 */
//@Configuration
//@ComponentScan
//@EnableTransactionManagement
public class TransactionTests {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TransactionTests.class);
        BookService bean = context.getBean(BookService.class);
        bean.batchInsert("java", "payth", "react");




    }

    /**
     * 定义一个数据源
     *
     * @return
     */
    //@Bean
    //@ConditionalOnMissingBean
    public DataSource dataSource() {
        DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("com.mysql.cj.jdbc.Driver");
        dataSourceBuilder.url("jdbc:mysql://127.0.0.1:3306/lunwen?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai");
        dataSourceBuilder.username("root");
        dataSourceBuilder.password("123456");

        return dataSourceBuilder.build();
    }

    /**
     * 定义个模板
     *
     * @return
     */
    //@Bean
    public JdbcTemplate template(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    //@Bean
    //@ConditionalOnMissingBean
    public PlatformTransactionManager manager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
